package com.qfmy.service.impl.novel;

import com.qfmy.mapper.novel.CategoryMapper;
import com.qfmy.service.novel.CategoryService;
import com.qfmy.vo.novel.CategoryVo;
import model.entity.novel.Category;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 清风明月
 * @Package com.qfmy.service.impl.novel
 * @date 2025/6/6
 * @description 小说分类
 */
@Service
@SuppressWarnings("all")
public  class CategoryServiceImpl implements CategoryService {

    /**
     * 注入mapper
     */
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 注入redisTemplate
     */
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 获取所有的分类标签
     * @return
     */
    @Override
    public List<String> getAllCategoryTags() {
        List<String> categoryTags = categoryMapper.getAllCategoryTags();
        return categoryTags;
    }

    /**
     * 根据分类类型获取分类小说
     * @param type
     * @return
     */
    @Override
    public List<CategoryVo> getCategoryNovels(Integer type) {
        //创建缓存key
        String key = "categoryNovels:" + type;
        List<CategoryVo> categoryNovels = (List<CategoryVo>) redisTemplate.opsForValue().get(key);
        if (categoryNovels != null) {
            return categoryNovels;
        }
        synchronized (this)
        {
           //再次查询缓存
            categoryNovels = (List<CategoryVo>) redisTemplate.opsForValue().get(key);
            if(categoryNovels != null)
            {
                return categoryNovels;
            }
            //查询数据库
            List<Category> ct = categoryMapper.getCategoryNovels(type);
            //判断是否为空
            if(categoryNovels == null)
            {
                categoryNovels = new ArrayList<>();
                redisTemplate.opsForValue().set(key, categoryNovels,5, TimeUnit.MINUTES);
            }
            //对数据进行拷贝
            categoryNovels = Copy(ct);
            //存放到缓存里面
            redisTemplate.opsForValue().set(key, categoryNovels, 2, TimeUnit.HOURS);
        }
        return categoryNovels;
    }

    /**
     * 对数据进行拷贝
     * @param categoryNovels
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<CategoryVo> Copy(@NotNull List<Category> categoryNovels) {
        //创建集合
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category : categoryNovels) {
            //创建对象
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setCategoryName(category.getCategoryName());
            categoryVo.setImage(category.getImage());
            categoryVo.setCount(category.getCount());
            categoryVos.add(categoryVo);
        }
        return categoryVos;
    }
}
